package net.cxd.reflect;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ScanAll {
	public static <T>  boolean getClassIsAnn(Class<T> clazz,Class<Annotation> ann) throws Exception{
		Annotation an = clazz.getAnnotation(ann);
		return an == null ? false : true;
	}
	public static <T> List<T> getAllClassWidthAnn(List<T> all ,Class<Annotation> ann) throws Exception{
		List<T> classes = new ArrayList<T>();
		for (T t :all) {
			if (getClassIsAnn(t.getClass(), ann)) {
				classes.add(t);
			}
		}
		return classes;
	}
	public static <T> Map<String, Object> allMthodWith(List<T> all , Class<Annotation> ann) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		for (T obj : all) {
			Method[] mts = obj.getClass().getMethods();
			if (mts == null ) {
				mts = obj.getClass().getDeclaredMethods();
			}
			if (mts != null) {
				for (Method method : mts) {
					if (method.getAnnotation(ann) != null) {
						map.put(method.getName(), obj);
					}
				}
			}
		}
		return map;
	}
	
	public static <T> Map<String, Object> getAll(List<T> all , Class<Annotation> classAnn , Class<Annotation> methodAnn) throws Exception{
		List<T> list = getAllClassWidthAnn(all, classAnn);
		return allMthodWith(list, methodAnn);
	}
	
	
	
}
